Skip to content

Conversation

@quentinus95
Copy link

@quentinus95 quentinus95 commented Feb 6, 2020

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tickets #3389
License MIT
Doc PR

While I understand what is the logic under the code I'm removing, I believe this check is wrong because it assumes the property belongs to a schema describing a model for a write operation.

In that case, we could say the field is not required because it is not writable.

However, when describing a model for a read operation (e.g. using a group in the normalizationContext), we do want read-only fields to be marked as required. Otherwise tooling such as code generators will assume the field is nullable.

This is especially happening for all identifiers when auto-generated in the backend. They are always read-only but they are also always required (the client should not assume that the id field may be null).

@quentinus95
Copy link
Author

quentinus95 commented Feb 6, 2020

Also I'm facing the same issue when I have a virtual property using a getter:

    /*     
     * @ApiProperty(
     *     required=true
     * )
     */
    public function getSomething()
    {
    }

This won't set the property to required due to the 4 lines of code I'm removing.

}

public function testShouldReturnRequiredFalseWhenRequiredTrueIsSetButMaskedByWritableFalse()
public function testShouldReturnRequiredTrueEvenIfWritableFalseSoClientCanAssumeValueCannotBeUndefinedOnReadOperation()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should remove / rephrase this test

@quentinus95
Copy link
Author

Failing tests are unrelated.

@soyuka
Copy link
Member

soyuka commented Feb 18, 2020

I'm unsure about this, what you say makes sense, wdyt @api-platform/core-team ?

@quentinus95
Copy link
Author

@soyuka is there anything I can do (more explanations, implementation change, ...) to get some feedback and know if this branch can/will be merged in the future?

Thanks!

@soyuka
Copy link
Member

soyuka commented Apr 15, 2020

I agree with this code, I'm just afraid that such change could break some implementations... I'd like to understand why we would need writable here as well.

@alanpoulain
Copy link
Member

Isn't the issue the assumption of nullability if the property is not required?
This PR is also related: #3518
We only have the specification of JSON Schema to understand it (https://tools.ietf.org/html/draft-bhutton-json-schema-validation-00#section-6.5.3):

An object instance is valid against this keyword if every item in the array is the name of a property in the instance.

It only says that the property may not appear, not that it is nullable.

But I think you are right to say that if a property is always normalized, then it should probably be required.

@mvlad
Copy link

mvlad commented Feb 4, 2022

This bug also leads to the wrong documentation when constructor property promotion is used.

For example:

<?php

namespace App\Customer\ApiResource;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints\NotBlank;

#[ApiResource(
    collectionOperations: ['post'],
    itemOperations: [],
)]
class Book
{
    public function __construct(
        #[NotBlank]
        #[ApiProperty(identifier: true)]
        protected string $name,
    ) {
    }

    public function getName(): string
    {
        return $this->name;
    }
}

Here the name property is required and ApiPlatform can set it. But in the documentation, this property is not marked as "required" because it's not writable.

@roman-eonx
Copy link

It's been a while since this PR was created but the issue is still actual. Especially with properties set from constructors in our use case. @soyuka what is required to continue working on this issue? Please let us know if you want a fresh PR for the current version to be created.

@stale
Copy link

stale bot commented Nov 4, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Nov 4, 2022
@stale
Copy link

stale bot commented Jan 3, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 3, 2023
@stale stale bot closed this Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants